package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserlikeApiImpl implements UserlikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存或更新
     *
     * @param userId：当前操作用户的id
     * @param likeUserId：喜欢的用户的id
     * @param
     * @return
     */
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean flag) {
        try {
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if (userLike != null) {
                //更新
                Update update = Update.update("isLike", flag)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);

            } else {
                //保存
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setIsLike(flag);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map counts(Long userId) {
        List<UserLike> eachLike = findEachLike(userId);
        List<UserLike> userLikes = getUserLikes(userId);
        List<UserLike> likeMeUser = getLikeMeUser(userId);
        Map map = new HashMap();
        map.put("eachLoveCount",eachLike.size());
        map.put("loveCount", userLikes.size());
        map.put("fanCount", likeMeUser.size());
        return map;
    }

    /**
     * 查询互相喜欢的用户
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserLike> findEachLike(Long userId) {
        List<UserLike> userLikes = getUserLikes(userId);
        if (CollUtil.isEmpty(userLikes)) {
            return new ArrayList<>();
        }
        List<Long> userIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Query query = Query.query(Criteria.where("userId").in(userIds).and("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> eachLikes = mongoTemplate.find(query, UserLike.class);
        return eachLikes;
    }

    /**
     * 查询我喜欢的用户
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserLike> getUserLikes(Long userId) {
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId).and("isLike").is(true)).with(Sort.by(Sort.Order.desc("created"))), UserLike.class);
        return userLikes;
    }

    /**
     * 查询喜欢我的用户
     */
    @Override
    public List<UserLike> getLikeMeUser(Long userId) {
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true)).with(Sort.by(Sort.Order.desc("created"))), UserLike.class);
        return userLikes;
    }

    /**
     * 查询当前用户是否喜欢指定用户
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public Boolean likeOrNot(Long id, Long userId) {
        boolean exists = mongoTemplate.exists(Query.query(Criteria.where("userId").is(id)
                .and("likeUserId").is(userId)
                .and("isLike").is(true)), UserLike.class);
        return exists;
    }
}
